<?php
namespace backend\controllers;

use yii\web\AdminController;
use backend\models\UploadForm;
use Yii;
use backend\models\GoodsKeyword;

class GoodsKeywordController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'GoodsKeyword';
    //商品关键字列表
    public function actionIndex(){
        $model = new UploadForm();
        return $this->render('index',['model'=>$model]);
    }
    
    public function actionGetKeyword(){
        $data = Yii::$app->request->post();
        $name = $data['goods_keyword_name'];
        $query = GoodsKeyword::find();
        if ($name){
            $query = $query->where(['like','name',$name]);
        }
        $count = $query->count();
        $sort_string = empty($data['columns'][$data['order'][0]['column']]['data']) ? 'id' : $data['columns'][$data['order'][0]['column']]['data'];
        $list = $query->offset($data['start'])->limit($data['length'])->orderBy(
            [$sort_string=>$data['order'][0]['dir'] == 'asc' ? SORT_ASC : SORT_DESC])->asArray()->all();
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $list;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionChangeKeyword(){
        $model = new GoodsKeyword();
        $data = Yii::$app->request->post();
        $id = $data['keyword_id'];
        $use_flg = $data['use_flg'];
        $name = $data['name'];
        $sort = $data['sort'];
        $oper = $model->getCreator();
        $operTime = date('Y-m-d H:i:s');
        $has_keyword = $model->find()->where('name=:name',[':name'=>$name])->asArray()->one();
        if ($id){
            if (count($has_keyword) > 0 && $id != $has_keyword['id']){
                $this->retJson('关键字已存在',500);
            }
            $model = $model->findOne($id);
            $model->editor = $oper;
            $model->edit_time = $operTime;
        }else{
            if (count($has_keyword) > 0){
                $this->retJson('关键字已存在',500);
            }
            $model->creator = $oper;
            $model->create_time = $operTime;
        }
        $model->name = $name;
        $model->sort = $sort;
        $model->use_flg = $use_flg;
//         if (!$use_flg){
//             $model->is_publish = 0;
//         }
        $model->save();
        $keyword_id = $model->attributes['id'];
        if (!$keyword_id){
            $this->retJson('操作失败',500);
        }
        $this->retJson('操作成功',200);
    }
    
    public function actionChangeKeywordStatus(){
        $where = $data_now = [];
        $model = new GoodsKeyword();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $status = intval($data['status']);
        $type = intval($data['type']);
        if ($type == 1){
            $data_now['use_flg'] = $status;
//             if (!$status){
//                 $data_now['is_publish'] = 0;
//             }
        }else{
            //$data_now['is_publish'] = $status;
        }
        $data_now['editor'] = $model->getCreator();
        $data_now['edit_time'] = date('Y-m-d H:i:s');
        $where['id'] = $id;
        $ret = $model::updateAll($data_now,$where);
        if(!$id){
            $this->retJson('操作失败',500);
        }
        $this->retJson('操作成功',200);
    }
}